package com.kenliang.javase.concurrent.thread;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author KenLiang
 * @description:
 * @date Created in 12:02 PM 2021/1/12.
 */
public class ThreadPoolTest {

    final ScheduledExecutorService executorService=null;
    final ThreadPoolExecutor threadPoolExecutor;

    public ThreadPoolTest() {
        System.out.println(Runtime.getRuntime().availableProcessors());
        threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), 10, 0, TimeUnit.NANOSECONDS,
                new ArrayBlockingQueue<Runnable>(8), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker.longPolling.public" + t.getId());
                t.setDaemon(true);
                return t;
            }
        }, new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println("reject");
            }
        });
    }

    class PureLongPollingRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println(new Date().toString() +"----x"+Thread.currentThread().getName()+":"+Thread.currentThread().getId());
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }



    public static void main(String[] args) throws IOException {
        ThreadPoolTest scheduledThreadPoolTest = new ThreadPoolTest();
//        scheduledThreadPoolTest.executorService.execute(scheduledThreadPoolTest.new LongPollingRunnable());

        PureLongPollingRunnable pureLongPollingRunnable = scheduledThreadPoolTest.new PureLongPollingRunnable();
        //10个线程（最大线程数）
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        //两个被拒绝的线程  最大线程数+队列大小之外的线程
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        //队列内的线程（8个）
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        scheduledThreadPoolTest.threadPoolExecutor.execute(pureLongPollingRunnable);
        System.in.read();
    }

}

